package com.marce.remis.flexClient.components.base
{
	import com.marce.remis.flexClient.components.CarBar;
	import com.marce.remis.flexClient.components.MyPanel;
	import com.marce.remis.flexClient.control.IconLocator;
	import com.marce.remis.flexClient.model.ModelLocator;
	import com.marce.remis.flexClient.model.RemisesModel;
	import com.marce.remis.flexClient.view.CarBarPanel;
	import com.marce.remis.flexClient.vo.Remis;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import mx.containers.HBox;
	import mx.containers.Panel;
	import mx.core.Application;
	import mx.events.FlexEvent;

	public class CarBarBase extends HBox
	{
		private var _view:CarBar;
		private var openWidth:Number;
		private var opened:Boolean;
		private var arrastrando:Boolean;
		private var moviendo:Boolean;
		private var ultimoY:Number = NaN;
		private var selectedCar:Panel = null;
		
		private var tiposSelectedByDefault:Dictionary = new Dictionary();
		private var tiposDefaultBySelected:Dictionary = new Dictionary();
		
		private var model:RemisesModel;
		public function CarBarBase()
		{
			super();
			this._view = this as CarBar;
			
			model = ModelLocator.instance.remisesModel;
			
			this.addEventListener(FlexEvent.CREATION_COMPLETE,onInit,false,0,true);
			tiposSelectedByDefault["salidaPanel"] = "salidaPanelSelected";
			tiposSelectedByDefault["pasajeroPanel"] = "pasajeroPanelSelected";
			tiposSelectedByDefault["librePanel"] = "librePanelSelected";
			tiposSelectedByDefault["basePanel"] = "basePanelSelected";
			tiposSelectedByDefault["sinPasajeroPanel"] = "sinPasajeroPanelSelected";
			tiposSelectedByDefault["desabledPanel"] = "desabledPanel";
			
			tiposDefaultBySelected["salidaPanelSelected"] = "salidaPanel";
			tiposDefaultBySelected["pasajeroPanelSelected"] = "pasajeroPanel";
			tiposDefaultBySelected["librePanelSelected"] = "librePanel";
			tiposDefaultBySelected["basePanelSelected"] = "basePanel";
			tiposDefaultBySelected["sinPasajeroPanelSelected"] = "sinPasajeroPanel";
			tiposDefaultBySelected["desabledPanel"] = "desabledPanel";
		}
		
		private function onInit(event:Event):void{
			this._view.container.addEventListener(MouseEvent.MOUSE_DOWN,onClickBar,false,0,true);
			this._view.container.addEventListener(MouseEvent.MOUSE_UP,onMouseUp,false,0,true);
			this._view.container.addEventListener(MouseEvent.MOUSE_OUT,onMouseOut,false,0,true);
			
			for each(var remis:Remis in model.remises){
				var panel:CarBarPanel = new CarBarPanel();
				panel.data = remis;
				panel.fija = true;
				panel.title = remis.chofer.apellido+" "+remis.chofer.nombre;
				panel.styleName = remis.estado+"Panel";
				panel.addEventListener(MouseEvent.MOUSE_MOVE,onMove,false,0,true);
				panel.addEventListener(MouseEvent.MOUSE_OVER,onMouseOver,false,0,true);
				this._view.containerBox.addChild(panel);
			}
			
			this._view.manija.source = IconLocator.MANIJA_ABIERTA;
			openWidth = this._view.container.width;
			opened = true;
		}
		
		protected function onManijaClick():void{
			opened = !opened;
			if(opened) abrir();
			else cerrar();
		}
		
		private function abrir():void{
			this._view.resizator.widthTo = openWidth;
			this._view.container.width = openWidth;
			this._view.manija.source = IconLocator.MANIJA_ABIERTA;
		}
		
		private function cerrar():void{
			this._view.resizator.widthTo = 0;
			this._view.container.width = 0;
			this._view.manija.source = IconLocator.MANIJA_CERRADA;
		}
		
		private function seleccionarCar(panel:MyPanel=null):void{
			if(!panel){
				if(selectedCar){
					selectedCar.styleName = tiposDefaultBySelected[selectedCar.styleName];
					selectedCar = null;
				}
				return;
			}	
			if(selectedCar){
				selectedCar.styleName = tiposDefaultBySelected[selectedCar.styleName];
				selectedCar = null;
			}
			panel.styleName = tiposSelectedByDefault[panel.styleName];
			selectedCar = panel;
		}
		
		protected function onClickBar(event:MouseEvent):void{
			arrastrando = true;
			seleccionarPanel(event);
		}
		
		protected function onMouseUp(event:MouseEvent):void{
			arrastrando = false;
			ultimoY = NaN;
			seleccionarCar();
		}
		
		private function onMouseOut(event:MouseEvent):void{
			if(this._view.container.mouseX<=0 || this._view.container.mouseY<=0){
				if(arrastrando && selectedCar){
					selectedCar.parent.removeChild(selectedCar);
					(Application.application as RemiseriaV2).addPanel(selectedCar);
					(selectedCar as CarBarPanel).fija = false;
				}
				onMouseUp(event);
			}
		}
		
		protected function onMove(event:MouseEvent):void{
			this.seleccionarPanel(event);
			if(arrastrando && !moviendo){
				if(isNaN(ultimoY)) ultimoY = this._view.container.mouseY;
				this._view.containerBox.y+= this._view.container.mouseY - ultimoY;
				trace("moviendo: "+(this._view.container.mouseY-ultimoY));
				ultimoY = this._view.container.mouseY;
				moviendo = true;
				var timer:Timer = new Timer(100,1);
				timer.addEventListener(TimerEvent.TIMER,onTimer,false,0,true);
				timer.start();
			}
		}
		
		private function onTimer(event:Event):void{
			moviendo = false;
		}
		
		private function seleccionarPanel(event:MouseEvent):void{
				seleccionarCar(event.currentTarget as CarBarPanel);
		}
		
		private function onMouseOver(event:MouseEvent):void{
			this.seleccionarPanel(event);
		}
		
	}
}